import { jobRangedAttack } from "../../../../job/ranged_attack";
import { getMaxEnergy, spawnCreep } from "../../../../role/spawn";
import { GroupTerminal } from "../../terminal_group";

export class GroupRangedAttackers extends GroupTerminal{
    get maxSize(){
        return 2;
    }
    serialize(){
        return {
            super: super.serialize(),
        }
    }
    deserialize(obj){
        super.deserialize(obj.super);
        return this;
    }
    spawn(){
        let newCreep = spawnCreep(this.spawnRoom,{
            require: [MOVE, RANGED_ATTACK],
            move: .50,
            ranged_attack: .50,
        },{
            memory: {
                role: 'ranged_attacker',
            }
        } ,getMaxEnergy(this.spawnRoom));
        if(!(newCreep instanceof Creep)){
            return newCreep;
        }
        this.addCreep(newCreep);
        return OK;
    }
    run(targets){
        for(let creep of this.creeps){
            jobRangedAttack.run(creep, targets);
        }
    }
    /**
     * 
     * @param {Creep} creep 
     * @param {Array<AnyCreep | AnyStructure>} targets 
     */
    creepRun(creep, targets){
        if(targets.length===0){
            return jobSpread.run(creep);
        }
        jobGoto.run(creep, targets, {visualizePathStyle:{stroke:"#ff0000"}});
        let ret;
        for(let target of targets){
            if(target.pos.isNearTo(creep)){
                ret = creep.rangedMassAttack();
            }else{
                ret = creep.rangedAttack(target);
            }
            if(ret === OK){
                break;
            }
        }
        return ret;
    }
}